package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.BorrowInfoEntity;
import com.cheng.data.mapper.BorrowInfoMapper;
import com.cheng.data.vo.BorrowInfoVO;
import com.cheng.data.vo.NoticeVO;
import com.cheng.data.vo.StatisticsVO;
import com.cheng.service.BorrowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/12/20 20:12
 * @version: 1.0
 * @description: 借阅信息处理
 */
@Service
public class BorrowServiceImpl implements BorrowService {

    private final static Logger log = LoggerFactory.getLogger(BorrowServiceImpl.class);

    @Autowired
    private BorrowInfoMapper borrowInfoMapper;

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: void
     * @date: 2021/12/20
     * @description: 新增借阅信息
     */
    @Override
    public void insertBorrowInfo(BorrowInfoEntity entity) throws CommonException {
        borrowInfoMapper.insert(entity);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: java.util.List<com.cheng.data.vo.BorrowInfoVO>
     * @date: 2021/12/20
     * @description: 查询待归还的借阅信息
     */
    @Override
    public List<BorrowInfoVO> queryBorowInfoList(Map<String, Object> map) throws CommonException {
        return borrowInfoMapper.queryBorrowInfoVOList(map);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: void
    * @date: 2021/12/20
    * @description: 修改借阅信息
    */
    @Override
    public void updateBorrowInfo(BorrowInfoEntity entity) throws CommonException {
        borrowInfoMapper.updateById(entity);
    }

    /**
     * @author: ChengJW
     * @params: [vo]
     * @return: com.cheng.data.entity.BorrowInfoEntity
     * @date: 2021/12/20
     * @description: 查询借阅信息
     */
    @Override
    public BorrowInfoEntity queryBorrowInfo(BorrowInfoVO vo) throws CommonException {
        QueryWrapper<BorrowInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(vo.getBwBussId()),"bw_Buss_Id",vo.getBwBussId());
        wrapper.eq(StringUtil.isNotNull(vo.getBwId()),"bw_Id",vo.getBwId());
        List<BorrowInfoEntity> list = borrowInfoMapper.selectList(wrapper);
        if (list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: com.cheng.data.vo.StatisticsVO
     * @date: 2021/12/21
     * @description: 统计借阅信息
     */
    @Override
    public List<StatisticsVO> queryStatisticsBrowVO(Map<String, Object> map) throws CommonException{
        if (map == null){
            return null;
        }
        if (CodeUtil.STATISTIC_TYPE.equals(map.get(CodeUtil.STATISTIC_CODE))){
            return borrowInfoMapper.queryStatisticsBrowVOList(map);
        }
        return borrowInfoMapper.queryStatisticsBrowVOListByCode(map);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: java.util.List<com.cheng.data.vo.NoticeVO>
     * @date: 2021/12/21
     * @description: home 页面关于借阅信息的提醒
     */
    @Override
    public NoticeVO queryBorrowInfoNoticeVO(Map<String, Object> map) throws CommonException{
        if (map == null){
            return null;
        }
        return borrowInfoMapper.queryBorrowInfoNoticeVO(map);
    }
}
